import { message, Result } from 'antd';
import { useEffect } from 'react';
import useOAuthSession from '@/pages/oidc/utils/useOAuthSession';
import useOAuthConfig from '@/pages/oidc/utils/useOAuthConfig';

export default function Login() {
  useEffect(() => {
    useOAuthConfig()
      .then((config) => {
        const session = useOAuthSession(true);

        const params = [
          'response_type=' + config.responseType,
          'state=' + session.state,
          'client_id=' + config.clientId,
          'scope=' + config.scope,
          'redirect_uri=' + encodeURIComponent(config.redirectUri),
          'code_challenge=' + session.codeChallenge,
          'code_challenge_method=S256',
        ];
        window.location.href =
          config.oidcConfig.authorization_endpoint + '?' + params.join('&');
      })
      .catch((err) => {
        message.error('获取登录信息错误');
      });
  }, []);

  return <Result title="登录中..." />;
}
